<template>
  <transition name="modal-fade" @after-leave="closed">
    <div v-show="show" class="base-modal-mask" @click.self="maskClose">
      <div
        :class="{
          'base-modals': true,
          transition__container: true,
        }"
        :style="{ width: width, borderRadius: isShowRadius ? 0 : '9px'}"
      >
        <div class="base-modal-header">
          <slot name="header">
            <slot name="title">
              <p v-if="title">
                {{ title }}
                <slot name="describe"></slot>
              </p>
            </slot>
            <slot name="operate"></slot>
            <button
              type="button"
              class="base-modal-header-close"
              @click="cancel"
              v-if="closeable"
            >
              <i class="iconfont icon-fe-close"></i>
            </button>
          </slot>
        </div>
        <div :class="['base-modal-body', bodyClass]">
          <slot></slot>
        </div>
        <div class="base-modal-footer" v-if="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>

export default {
  name: 'code-modal',
  props: {
    show: {
      // 是否显示组件
      type: Boolean,
      default: false
    },
    isShowRadius:{
      type:Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '640px'
    },
    closeable: {
      // 是否能手动关闭
      type: Boolean,
      default: true
    },
    maskCloseable: {
      // 是否允许点击遮罩关闭
      type: Boolean,
      default: false
    },
    bodyClass: String,
    appendToBody: {
      // 是否将弹窗插入body中
      type: Boolean,
      default: false
    },
    /** 是否允许全屏 */
    allowFullscreen: {
      type: Boolean,
      default: false
    },
    /** 是否允许自动关闭 */
    allowAutoClose: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      isFullscreen: false
    };
  },
  mounted() {
    // document.addEventL.body-heightistener('keydown', this.escClose)

    if (this.appendToBody) {
      document.body.appendChild(this.$el);
    }
  },
  destroyed() {
    // document.removeEventListener('keydown', this.escClose)

    // if appendToBody is true, remove DOM node after destroy
    if (this.appendToBody && this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el);
    }
  },
  methods: {
    closed() {
      this.$emit('closed'); // 关闭动画结束时触发
    },
    close() {
      this.$emit('close'); // 关闭时触发
      // 兼容sync
      if(this.allowAutoClose) {
        this.$emit('update:show', false);
      }
    },
    cancel() {
      this.$emit('cancel'); // 点击关闭按钮，或遮罩层关闭时触发
      this.close();
    },
    maskClose() {
      if (this.closeable && this.maskCloseable) {
        this.cancel();
      }
    },
    escClose(event) {
      if (this.show && this.closeable && event.keyCode == 27) {
        this.cancel();
      }
    },
  }
};
</script>

<style lang="scss" scoped>
.overflow-body-for-modal {
  overflow: hidden;
}

.base-modal-mask {
  @include mask();
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 99;
  overflow: auto;
}

.base-modal-header {
  background-color: #424647 !important;
  height: 44px;
  position: relative;
  padding: 10px 15px 10px 20px;
  font-size: 16px;
  color: $text-color-primary;
  font-weight: normal;

  display: flex;
  align-items: center;
  box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.15);

  p {
    flex: 1;
    margin: 0;
    height: 24px;
    line-height: 24px;
    font-size: 16px;
    font-weight: 600;
    font-size: 14px;

    @include text-ellipsis();
    color: #fff;
  }

  button {
    height: 24px;
    line-height: 24px;
    width: 24px;

    padding: 0;
    margin: 0;
    outline: none;
    color: #666;
    border: none;
    background-color: transparent;
    transition: color ease 0.15s;

    i {
      font-size: 14px;
    }

    &:hover {
      color: $color-primary;
    }

  }
  .base-modal-header-close {
    height: 25px;
    color: #fff;
    background-color: #606464;
    border-radius: 2px;
    i {
      //border: 1px solid #ccc;
      padding: 4px 5px;
    }
  }
  button + button {
    margin-left: 5px;
  }
}

.base-modal-body {
  flex: 1;
  overflow: auto;
}

//TODO: 统一边距
.base-modal-footer {
  min-height: 52px;
  display: flex;
  justify-content: flex-end;
  padding: 10px 20px;
  border-top: 1px solid $color-border-l2;
}

.base-modal-text-btn {
  outline: none;
  border: none;
  background-color: transparent;
  color: $text-color-secondary;

  &:hover {
    background-color: #f0f0f0;
  }

  &:disabled {
    cursor: not-allowed;
  }

  & + button {
    margin-left: 10px;
  }
}
.base-modals {
  position: relative;
  background-color: #fff;
  -moz-border-radius: 1px;
  border-radius: 1px;
  -moz-box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.15);
  box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.15);
  -moz-border-radius: 8px;
  border-radius: 8px;
  overflow: hidden;
  display: -webkit-flex;
  display: -moz-box;
  display: flex;
  flex-direction: column;
  height: 100vh;
}
</style>
